package com.action.pc.city;

import com.bean.City;
import com.bean.Prop_config;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.github.pagehelper.Page;
import com.service.city.CityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * 城市控制器
 *
 * @author 胡涛
 * @date 2018/10/5
 */
@Controller
@RequestMapping("city")
public class CityAction extends BaseAction {
    Logger log = Logger.getLogger(CityAction.class);
    @Autowired
    private CityService cityService;

    @RequestMapping("list")
    public ModelAndView list(ModelMap modelMap, CityVo vo ,HttpServletRequest request) throws UnsupportedEncodingException {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<String, Object>(16);
        paraMap.put("cityname", vo.getCityname());
        paraMap.put("contact", vo.getContact());
        paraMap.put("tel", vo.getTel());
        paraMap.put("starttime", vo.getStarttime());
        paraMap.put("endtime", vo.getEndtime());
        Page page = cityService.findByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/city_list", modelMap);
    }
    @RequestMapping("modify")
    public ModelAndView modify(HttpServletRequest request, ModelMap modelMap, CityVo vo) {
        Integer id = vo.getId();
        City city = new City();
        if (id != null) {
            city = cityService.findCityById(id);
        }
        modelMap.put("city", city);
        return new ModelAndView("/pc/city_modify", modelMap);
    }

    @RequestMapping("save")
    public ModelAndView save(HttpServletRequest request, ModelMap modelMap, CityVo vo) {
        City city = new City();
        city.setId(vo.getId());
        city.setCityname(vo.getCityname());
        city.setContact(vo.getContact());
        city.setTel(vo.getTel());
        city.setSort(vo.getSort());
        cityService.saveCity(city);
        if (vo.getId() != null) {
            return new ModelAndView("redirect:/city/list.htm", modelMap);
        } else {
            modelMap.put("id", city.getId());
            return new ModelAndView("redirect:/city/propconf.htm", modelMap);
        }
    }

    @RequestMapping("active")
    @ResponseBody
    public String active(HttpServletRequest request, ModelAndJson modelMap, CityVo vo) {
        Integer cityid = vo.getId();
        Integer enable = vo.getEnable();
        if (cityid != null && enable != null) {
            City city = new City();
            city.setId(cityid);
            city.setEnable(enable);
            cityService.update(city);
        }
        modelMap.setTrue("设置成功");
        return modelMap.toString();
    }

    /**
     * 业务配置
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("propconf")
    public ModelAndView propconf(HttpServletRequest request, ModelMap modelMap, CityVo vo) {
        Integer cityid = vo.getId();
        Prop_config prop = null;
        if (cityid != null) {
            prop = cityService.getPropByCityId(cityid);
        }
        if (prop == null) {
            prop = new Prop_config();
            prop.setCityid(cityid);
        }
        modelMap.put("prop", prop);
        return new ModelAndView("/pc/prop_conf", modelMap);
    }

    /**
     * 保存业务配置
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("savepropconf")
    public ModelAndView savepropconf(HttpServletRequest request, ModelMap modelMap, CityVo vo) {
        Prop_config prop = vo.getProp();
        if (prop != null) {
            cityService.saveProp(prop);
        }
        return new ModelAndView("redirect:/city/list.htm", modelMap);
    }
}
